<?php
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;


class User extends Authenticatable implements JWTSubject
{
    protected $table = 'users';
    protected $fillable = ['username','password'];
    protected $guard_name='api';
    public $timestamps = false;
    use HasRoles;
    use Notifiable;
    protected $hidden = ['password'];

    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public static function getUsers($where,$fields=['id','real_name','username','email'])
    {
        $rt = DB::table('users')->where('status', '=', $where['status'])
            ->select($fields)
            ->get()->toArray();
        return $rt;
    }

    /**
     * $where['page'] :offset
     * $where['length'] :每页长度
     * $where['keywords'] :搜索词
     * @param $where
     * @return array
     */
    public static function getList($where)
    {
        $page = $where['page'] - 1;
        $length = $where['length'];
        $keywords = "%" . $where['keywords'] . "%";

        $sql="select u.id, u.username, u.real_name, u.email,u.status, group_concat(r.name) as role_name
                from users u left join model_has_roles mhr on u.id = mhr.model_id
                left join roles r on mhr.role_id = r.id
                where u.username like ?
                group by u.id limit ?,?";
        $rt = DB::select($sql, [$keywords, $page, $length]);
        return $rt;
    }

    /**
     * 条件查询菜单
     * @param $where
     * @param $flag 1:字段有id;0：字段没有id
     * @return array
     */
    public static function getListByWhere($where, $flag = 1)
    {
        if ($flag) {
            $rt = DB::table('users')->where('id', '<>', $where['id'])
                ->where(function ($query) use ($where) {
                    $query->where('username', '=', $where['username'])
                        ->orWhere('real_name', '=', $where['real_name'])
                        ->orWhere('email', '=', $where['email']);
                })->get()->toArray();
        } else {
            $rt = DB::table('users')
                ->where(function ($query) use ($where) {
                    $query->where('username', '=', $where['username'])
                        ->orWhere('real_name', '=', $where['real_name'])
                        ->orWhere('email', '=', $where['email']);
                })->get()->toArray();
        }
        return $rt;
    }

    //
    public function editByWhere()
    {

    }

}
